package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterEventData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarFuelChange;
import com.navinfo.platform.trip.common.arithmetic.enums.EventType;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.config.ConfigMap;

/**
 * 油液变化算法<br/>
 * 输出：统计、事件<br/>
 */
public class FuelChange extends AbstractArithmetic<VarFuelChange> {
    /**
     * 加油阀值（百分比，10代表10%）
     */
    private int chargeThreshold = ConfigMap.getInteger("Fuel_Charge_Threshold");
    /**
     * 漏油阀值（百分比，10代表10%）
     */
    private int leakThreshold = ConfigMap.getInteger("Fuel_Leak_Threshold");

    @Override
    public void calculate() {
        logger.debug("油液变化算法开始...");
        VarFuelChange varFuelChange = super.getObj();
        CommonData commonData = drivingData.getCommonData();

        //取目前油液百分比和燃油消耗总量
        Double curOilPer = commonData.getOilPercentage();
        Double curTotalOil = commonData.getTotalOil();

        //行程开始，当变化类型未确定时进行计算
        if(varFuelChange.getChangeType() == 0 && curOilPer!=null) {
            double lastPer = varFuelChange.getLastTripOilPer();
            double lastOil = varFuelChange.getLastTotalOil();

            double nowOil = curTotalOil==null ? lastOil : curTotalOil;

            //本次百分比和上次百分比均大于0时计算加油漏油，否则默认未变化
            if(curOilPer> 0.0 && lastPer> 0.0){
                double changePer = curOilPer - lastPer;
                double changeOil = nowOil - lastOil;
                varFuelChange.setChangePer(Math.abs(changePer));
                varFuelChange.setChangeOil(Math.abs(changeOil));

                //变化大于0且变化绝对值大于等于阀值时为加油
                if(changePer > 0 && varFuelChange.getChangePer()>= chargeThreshold){
                    varFuelChange.setChangeType(1);
                    calOilEvent(EventType.fuelCharge, changePer, changeOil);
                }
                //变化小于0且变化绝对值大于等于阀值时为漏油
                else if(changePer< 0 && varFuelChange.getChangePer()>= leakThreshold){
                    varFuelChange.setChangeType(2);
                    calOilEvent(EventType.fuelLeak, changePer, changeOil);
                }else{
                    varFuelChange.setChangeType(3);
                }
            }else{
                varFuelChange.setChangeType(3);
            }
        }

        logger.debug("油液变化算法结束.[terminalId:{}]", terminalId);
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarFuelChange var = super.getObj();
        statisticDataOut.setChargeFuelPercent(0);
        statisticDataOut.setChargeFuel(0);
        statisticDataOut.setLeakFuelPercent(0);
        statisticDataOut.setLeakFuel(0);

        if(var.getChangeType() == 1) {
            //加油量百分比  单位:百分比，范围:0~100
            statisticDataOut.setChargeFuelPercent((int) (var.getChangePer()));
            //无法取到油箱容积，无法计算加漏油升数
            statisticDataOut.setChargeFuel((int) var.getChangeOil());
        }else if(var.getChangeType() == 2) {
            //漏油量百分比  单位:百分比，范围:0~100
            statisticDataOut.setLeakFuelPercent((int) (var.getChangePer()));
            //无法取到油箱容积，无法计算加漏油升数
            statisticDataOut.setLeakFuel((int) var.getChangeOil());
        }
    }

    /**
     * 计算油液变化事件
     * @param type
     * @param changePer 燃油变化百分比
     * @param changeOil 燃油变化量
     */
    private void calOilEvent(EventType type, double changePer, double changeOil){
        if(isOutputEvent()){
            CommonData commonData = drivingData.getCommonData();
            OuterEventData eventData = new OuterEventData();
            eventData.setEvent(type);
            eventData.setStartGpsTime(getObj().getLastGpsTime());
            eventData.setStartGpsDirection(commonData.getGpsDirection());
            eventData.setStartLatitude(commonData.getLatitude());
            eventData.setStartLongitude(commonData.getLongitude());
            eventData.setStartHeight(commonData.getHeight());
            eventData.setStartSpeed(commonData.getSpeedKmh());
            eventData.setStopGpsTime(commonData.getGpsTime());
            eventData.setStopGpsDirection(commonData.getGpsDirection());
            eventData.setStopLatitude(commonData.getLatitude());
            eventData.setStopLongitude(commonData.getLongitude());
            eventData.setStopHeight(commonData.getHeight());
            eventData.setStopSpeed(commonData.getSpeedKmh());
            eventData.setDuration(eventData.getStopGpsTime() - eventData.getStartGpsTime());
            eventData.setExtendField06(changePer);
            eventData.setExtendField07(changeOil);

            //放入事件输出列表
            drivingData.addEventDataToList(eventData);
        }else{
            logger.info("FuelChange不输出事件");
        }
    }
}
